//
// Created by Wonderful on 2025/9/19.
//

#ifndef C___LEARN_TYPECONVERT_H
#define C___LEARN_TYPECONVERT_H


#include <iostream>
//#include"Test.h"

class TypeConvert {
};

//  用类型转换构造函数进行类型转换
//  转换构造函数，本质是一个构造函数，是只有一个参数的构造函数。


class Point3D;
class Point2D;

class Point3D{
public:
    void Print(){
        std::cout << "(" << x << "," << y << "," << z << ")" << std::endl;
    }
    Point3D(int x, int y, int z) : x(x), y(y), z(z) {}
    //  转换构造函数
    //  explicit: 以显示的方式完成转化
    explicit Point3D(Point2D & p);
    //  static_cast<Point3D>(p2);
    //  或 (Point3D)p2;

    //  可以=赋值，但不可以初始化
    Point3D & operator = (const Point2D & p2);
//    {
//        this->x = p2.x;
//        this->y = p2.y;
//        return *this;
//    }
    double operator += (double d);
public:
    int x, y, z;
};

class Point2D{
public:
    Point2D(int x, int y): x(x), y(y){}
    void Print(){
        std::cout << "(" << x << "," << y <<" )" << std::endl;
    }
    //  声明友元类
    friend Point3D;
    //  可以直接给Point3D初始化, 可以赋值(赋值时，如果要转换成的类型重载了=运算符， 则优先使用要转换类型的重载)
    operator Point3D();

   friend Point3D & Point3D::operator=(const Point2D & p2);
private:
    int x, y;
};

Point3D & Point3D::operator=(const Point2D &p2) {
    this->x = p2.x;
    this->y = p2.y;
    return *this;
}

Point3D::Point3D(Point2D &p){
    this->x = p.x;
    this->y = p.y;
    this->z = 0;
}

//  用类型转换操作符函数进行转换
//  转换函数必须是类方法，转换函数无参数，且不需要声明返回类型(因为转换类型就是要返回的类型)。
//  效果跟重载 = 运算符一致。
Point2D::operator Point3D(){
    return Point3D(x, y, 0);
}


double Point3D::operator+=(double d) {
    return this->x + d;
}


#endif //C___LEARN_TYPECONVERT_H
